#include "func.h"
#include "image.h"

// 导入图片文件
void getImage() {
	wh = newimage();
	bl = newimage();
	vwh = newimage();
	vbl = newimage();
	bk1 = newimage();
	ic = newimage();
	pl1 = newimage();
	pl2 = newimage();
	pl1w = newimage();
	pl2w = newimage();
	vs1 = newimage();
	ex = newimage();
	re = newimage();
	vs2 = newimage();
	logo = newimage();
	cb = newimage();
	win = newimage();
	gobang = newimage();
	dr = newimage();
	idex = newimage();
	bk = newimage();
	vx = newimage();
	getimage(wh, "./res/02.png");
	getimage(bl, "./res/01.png");
	getimage(vwh, "./res/04.png");
	getimage(vbl, "./res/03.png");
	getimage(bk, "./res/bk1.jpg");
	getimage(ic, "./res/88.png");
	getimage(pl1, "./res/pl1.png");
	getimage(pl2, "./res/pl2.png");
	getimage(pl1w, "./res/pl1w.png");
	getimage(pl2w, "./res/pl2w.png");
	getimage(ex, "./res/exit.png");
	getimage(vs1, "./res/vs.png");
	getimage(re, "./res/return.png");
	getimage(vs2, "./res/vsc.png");
	getimage(logo, "./res/logo.png");
	getimage(cb, "./res/cb.png");
	getimage(win, "./res/th.png");
	getimage(gobang, "./res/003.png");
	getimage(dr, "./res/dr.png");
	getimage(idex, "./res/000.png");
	getimage(vx, "./res/888.png");
}

// 删除图片文件
void delImage() {
	delimage(wh);
	delimage(bl);
	delimage(vwh);
	delimage(vbl);
	delimage(bk);
	delimage(ic);
	delimage(pl1);
	delimage(pl2);
	delimage(pl1w);
	delimage(pl2w);
	delimage(vs1);
	delimage(vs2);
	delimage(ex);
	delimage(re);
	delimage(logo);
	delimage(cb);
	delimage(win);
	delimage(gobang);
	delimage(idex);
	delimage(dr);
	delimage(vx);
}

// 打印棋盘
void drawboard() {
	putimage(0, 0, bk);
	setcolor(BLACK);
	setfillcolor(EGEARGB(140, 255, 255, 255));
	ege_fillrect(25, 25, 750, 750);
	for (int i = 1; i < 16; i++) {
		line(50, i * 50, 750, i * 50);
		line(i * 50, 50, i * 50, 750);
	}
}

// 打印单个棋子
void drawchess(int x, int y, PIMAGE player) {
	putimage_withalpha(NULL, player, x - 25, y - 25, 0, 0);
}

// 打印所有棋子
void chessInboard(char board[17][17]) {
	for (int i = 1; i <= 15; i++) {
		for (int j = 1; j <= 15; j++) {
			switch (board[i][j]) {
				case 1:
					drawchess(i * 50, j * 50, bl);
					break;
				case 2:
					drawchess(i * 50, j * 50, wh);
					break;
				default:
					break;
			}
		}
	}
}

// 判断鼠标是否在按钮内，如果在就加一层白色矩形
void flood(int x, int y, int l, int r, int b, int t, PIMAGE pic, int disl) {
	if (x > l && x < r && y > b && y < t) {
		setfillcolor(EGEARGB(140, 255, 255, 255));
		ege_fillrect(l + 1, b + 1, r - l - 2, t - b - 2);
		putimage_withalpha(NULL, pic, l + disl, b + 10, 0, 0);
	}
}

// 打印起始窗口
int stwindow() {
	int x, y, flag;
	char board1[17][17]; // 起始页面的棋盘
	initboard(board); // 初始化对局棋盘
	initboard(board1); //初始化起始页面棋盘

	for (int i = 0; i < 10; i++) {
		x = random(15) + 1;
		y = random(15) + 1;
		board1[x][y] = 1;
		x = random(15) + 1;
		y = random(15) + 1;
		board1[x][y] = 2;
	}

	for (; is_run(); delay_fps(80)) {
		mouse_msg msg;
		bool idx = 0;
		drawboard();
		xyprintf(800, 780, "最终解释权归钟宏展所有");
		chessInboard(board1);
//		putimage(200, 150, vx);
		setfillcolor(EGEARGB(100, 255, 255, 255));
		putimage_withalpha(NULL, ic, 800, 50, 0, 0);
		putimage_withalpha(NULL, gobang, 800, 400, 0, 0);
		rectangle(810, 200, 975, 245);
		putimage_withalpha(NULL, vs1, 820, 210, 0, 0);
		rectangle(810, 300, 975, 345);
		putimage_withalpha(NULL, vs2, 820, 310, 0, 0);
		rectangle(810, 700, 975, 745);
		putimage_withalpha(NULL, ex, 840, 710, 0, 0);

		mousepos(&x, &y);
		flood(x, y, 810, 975, 200, 245, vs1, 10);
		flood(x, y, 810, 975, 700, 745, ex, 30);
		flood(x, y, 810, 975, 300, 345, vs2, 10);
		while (mousemsg()) {
			msg = getmouse();
			if (msg.x > 810 && msg.x < 975 && msg.y > 200 && msg.y < 245 && msg.is_down() && msg.is_left()) {
				flag = 1;
				idx = 1;
				break;
			} else if (msg.x > 810 && msg.x < 975 && msg.y > 700 && msg.y < 745 && msg.is_down() && msg.is_left()) {
				flag = 0;
				idx = 1;
				break;
			} else if (msg.x > 810 && msg.x < 975 && msg.y > 300 && msg.y < 345 && msg.is_down() && msg.is_left()) {
				flag = 2;
				idx = 1;
				break;
			}
		}
		if (idx)
			break;
	}
	return flag;
}


// 初始化棋盘,将棋盘初始化为0
void initboard(char b[16][17]) {
	for (int i = 1; i <= 15; i++)
		for (int j = 1; j <= 15; j++)
			b[i][j] = 0;
}

// 打印赢家特效
void drawWin(int winner) {
	drawboard();
	chessInboard(board);

	putimage_withalpha(NULL, ic, 800, 50, 0, 0);

	rectangle(790, 200, 975, 245);
	putimage_withalpha(NULL, pl1, 800, 210, 0, 0);
	drawchess(880, 290, bl);

	rectangle(790, 360, 975, 405);
	putimage_withalpha(NULL, pl2, 800, 370, 0, 0);
	drawchess(880, 450, wh);

	rectangle(810, 700, 975, 745);
	putimage_withalpha(NULL, re, 860, 710, 0, 0);

	drawchess(prx * 50, pry * 50, idex);

	Sleep(1000);
	for (int x = 40; is_run(); delay_fps(80)) {
		cleardevice();

		drawboard();
		chessInboard(board);

		putimage_withalpha(NULL, ic, 800, 50, 0, 0);

		rectangle(790, 200, 975, 245);
		putimage_withalpha(NULL, pl1, 800, 210, 0, 0);
		drawchess(880, 290, bl);

		rectangle(790, 360, 975, 405);
		putimage_withalpha(NULL, pl2, 800, 370, 0, 0);
		drawchess(880, 450, wh);

		rectangle(810, 700, 975, 745);
		putimage_withalpha(NULL, re, 860, 710, 0, 0);

		drawchess(prx * 50, pry * 50, idex);

		if (x > 160)
			break;
		setfillcolor(EGEARGB(x, 160, 160, 160));
		ege_fillrect(0, 0, 1000, 800);
		x ++;
	}
	drawboard();
	chessInboard(board);

	putimage_withalpha(NULL, ic, 800, 50, 0, 0);

	rectangle(790, 200, 975, 245);
	putimage_withalpha(NULL, pl1, 800, 210, 0, 0);
	drawchess(880, 290, bl);

	rectangle(790, 360, 975, 405);
	putimage_withalpha(NULL, pl2, 800, 370, 0, 0);
	drawchess(880, 450, wh);

	rectangle(810, 700, 975, 745);
	putimage_withalpha(NULL, re, 860, 710, 0, 0);

	drawchess(prx * 50, pry * 50, idex);

	setfillcolor(EGEARGB(160, 160, 160, 160));
	ege_fillrect(0, 0, 1000, 800);

	putimage_withalpha(NULL, win, 315, 335, 0, 0);
	putimage_withalpha(NULL, logo, 350, 350, 0, 0);

	if (winner == 1)
		putimage_withalpha(NULL, pl1w, 460, 365, 0, 0);
	else if (winner == 2)
		putimage_withalpha(NULL, pl2w, 460, 365, 0, 0);
	else
		putimage_withalpha(NULL, dr, 460, 380);
	putimage_withalpha(NULL, cb, 430, 470, 0, 0);

	char ch;
	while (ch = getch())
		if (ch == ' ')
			break;
}
